leaguedf <- read_csv('../../data_sets/S13LeagueOfLegendsData.csv', 
                      col_types=c('c', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'c'), 
                      col_names=c('rowno', 'Name', 'Class', 'Role', 'Tier', 'Score', 'Trend', "WinRate", "RoleRate", "PickRate", "BanRate", 'KDA', 'Patch'), skip=1) %>%
  column_to_rownames("rowno") %>% 
  mutate(PickBanRate = PickRate + BanRate, Patch = as.numeric(str_replace(Patch, '(.*?)_(.*?)', ''))) %>%
  select(-c(PickRate, BanRate)) 


stats <- leaguedf %>% group_by(Name) %>%
  summarize(sdWinRate = sd(WinRate), sdPickBanRate = sd(PickBanRate))
leaguedf <- inner_join(stats, leaguedf, 'Name')

leaguedfRole <- leaguedf %>%
  mutate(Role = case_when(Role == "ADC" ~ "Attack Damage Carry", TRUE ~ str_to_title(Role)))
#SD by patch
Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

leaguedfRole %>%
  group_by(Name) %>% filter(Class != "NULL") %>%
  ggplot() + geom_point(mapping=aes(x=sdWinRate, y=sdPickBanRate, color = Class)) + facet_wrap(~ Role) + xlab("Standard Deviation of Win Rate") + ylab("Standard Deviation of Pick Ban Rate") + ggtitle("Volatility of Champions")

#Win rate and Pick Ban Rate over time seperated by Role
PatchRoleStats <- leaguedfRole %>% filter(Role != "Jungle") %>%
  group_by(Role, Patch) %>%
  summarize(meanWinRate = mean(WinRate), meanPBRate = mean(PickBanRate),.groups='keep')

plot1a <- leaguedfRole %>% filter(Role != "Jungle") %>%
  ggplot() + geom_line(mapping=aes(x=Patch, y=WinRate, color=Name, alpha=0.001)) +
  geom_line(data = PatchRoleStats, mapping = aes(x=Patch, y=meanWinRate), color="black")+ 
  theme(legend.position="none") + facet_wrap( ~ Role) + ggtitle("Win Rate over Time seperated by Role") + xlab('') + ylab("")

plot1b <- leaguedfRole %>% filter(Role != "Jungle") %>%
  ggplot() + 
  geom_line(mapping=aes(x=Patch, y=PickBanRate, color=Name, alpha=0.001)) + 
  geom_line(data = PatchRoleStats, mapping=aes(x=Patch, y=meanPBRate), color="black") + 
  theme(legend.position="above") + facet_wrap( ~ Role) + ggtitle("PBRate over Time Seperated by Role") + xlab('') + ylab("")
#Win Rate and Pick Ban Rate over Time for just Heimerdinger seperated by Role
plot2a <- leaguedfRole %>% filter(Name == "Heimerdinger") %>%
  ggplot() + geom_line(mapping = aes(x = Patch, y = WinRate, alpha=0.5), color="green") + 
  geom_line(data = PatchRoleStats, mapping=aes(x=Patch, y=meanWinRate), color="black") + 
  facet_wrap(~ Role) + theme(legend.position="none") + xlab("Patch") + ylab("")

plot2b <- leaguedfRole %>% filter(Name == "Heimerdinger") %>%
  ggplot() + geom_line(mapping = aes(x = Patch, y = PickBanRate), color="green") + 
  geom_line(data = PatchRoleStats, mapping=aes(x=Patch, y=meanPBRate), color="black") + 
  facet_wrap("Role") + theme(legend.position="none") + scale_x_discrete("Patch", labels = 1:24) + ylab('')


patched <- (plot1a & plot1b) / (plot2a & plot2b)
patched

Is this useful?????? What do you gain from it??

#Covariation of PBR for champions.

Spread_Data <- leaguedf %>% 
  pivot_wider(id_cols = c('Name', 'Role'), names_from='Patch', values_from='PickBanRate') %>%
  mutate(ID = paste(Name, Role, sep='.')) %>%
  select(-c(1,2)) %>%
  na.omit()

NameList = Spread_Data$ID

Spread_Data <- as_tibble(t(Spread_Data)) %>%
  filter(row_number() <= n() - 1) %>%
  mutate_if(is.character, as.numeric)
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if
## `.name_repair` is omitted as of tibble 2.0.0.
## ℹ Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
colnames(Spread_Data) <- NameList
CoVarMat <- as_tibble(cor(Spread_Data))
rownames(CoVarMat) <- colnames(CoVarMat)
## Warning: Setting row names on a tibble is deprecated.
df <- CoVarMat %>% select(Senna.SUPPORT, `Tahm Kench.SUPPORT`, Ashe.ADC) %>%
  gather(key = "Champion", value = "Covariance") %>%
  mutate(label = case_when(
    Champion == 'Tahm Kench.SUPPORT' & Covariance >= 0.97 ~ "Tahm Kench TOP",
    Champion == 'Tahm Kench.SUPPORT' & abs(Covariance) > 0.68 ~ "Senna Support",
    TRUE ~ as.character(NA)
  )) %>%
  filter(Covariance < 1)

ggplot(data = df, mapping = aes(x=Champion, y = Covariance)) + 
  geom_boxplot() + 
  ylab("Pick Ban Rate Covariance") + 
  ggtitle("PBR Covariance Boxplot") + 
  geom_text(aes(label = label), na.rm = TRUE, hjust = -0.1, size = 3) + 
  scale_x_discrete(name = c("Ashe ADC", "Senna Support", "Tahm Kench Support"))

p <- leaguedf %>%
  select("Name", "PickBanRate", "WinRate", "Role", "RoleRate", "Class", "Patch") %>% 
  filter(!(Class == "NULL")) %>%
  ggplot() + geom_point(mapping= aes(x = PickBanRate, y = WinRate, size=RoleRate, color=Class)) + 
  facet_wrap(~ Role) + xlab("Pick Ban Rate") + ylab("Win Rate")
gif <- p + transition_states(Patch, 
                             transition_length = 3, 
                             state_length= 1) + 
  labs(title = "Patch: {closest_state}")
anim <- animate(gif, fps = 30, nframes = 480)
anim
leaguedf %>%
  select("Name", "PickBanRate", "WinRate", "Role", "RoleRate", "Class", "Patch") %>%
  filter(!(Class == "NULL")) %>%
  group_by(Role) %>%
  group_map( ~ plot_ly(data = .,
      x = ~ PickBanRate,
      y = ~ WinRate,
      color = ~ Class,
      text = ~ Name,
      frame = ~ Patch, 
      hoverinfo = "text",
      type = "scatter",
      mode = "markers", 
      marker = list(size = ~ RoleRate*5)
      ), .keep = TRUE) %>%
  subplot(nrows = 2, shareX = TRUE, shareY=TRUE, margin=0.03) %>%
  layout(showlegend = FALSE, title = 'Pick Ban Rate vs. Win Rate by Patch seperated by Role',
         plot_bgcolor='#e5ecf6', 
         xaxis = list( 
           zerolinecolor = '#ffff', 
           zerolinewidth = 2, 
           gridcolor = 'ffff'), 
         yaxis = list( 
           zerolinecolor = '#ffff', 
           zerolinewidth = 2, 
           gridcolor = 'ffff'),
         margin = 0.07) %>%
  layout(annotations = annotations)
#Correlation plots for all questions

plot1a <- leaguedf %>%
  ggplot() + geom_point(x = PickBanRate)